package com.xinou.daily_report.controller;

import com.alibaba.excel.EasyExcel;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.database.BasePO;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.FruitPO;
import com.xinou.daily_report.PO.FruitUrgePO;
import com.xinou.daily_report.VO.WeekPlanExcelVO;
import com.xinou.daily_report.VO.fruit.FruitCountVO;
import com.xinou.daily_report.VO.fruit.FruitExcelVO;
import com.xinou.daily_report.VO.fruit.FruitInfoVO;
import com.xinou.daily_report.VO.fruit.FruitListVO;
import com.xinou.daily_report.mapper.SysFruitMapper;
import com.xinou.daily_report.service.SysFruitService;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/admin/fruit")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"后台水果基金"})
public class SysFruitController {
    private final SysFruitService sysFruitService;
    private final SysFruitMapper sysFruitMapper;
    private final FileUploadService fileUploadService;
    public static final String UploadFileUrl="https://image.xiaokesi.com/";

    @PostMapping("/list")
    @ApiOperation("获取水果基金列表")
    @RequiresPermissions({"admin:fruit:list"})
    public ApiResponse<FruitListVO> fruitList(@RequestBody FruitPO po){
        return sysFruitService.fruitList(po);
    }

    @PostMapping("/urge")
    @ApiOperation("催缴水果基金")
    @RequiresPermissions({"admin:fruit:urge"})
    public ApiResponse<Void> fruitUrge(@RequestBody FruitUrgePO po){
        return sysFruitService.fruitUrge(po);
    }

    @PostMapping("/urgeConfirm")
    @ApiOperation("确认催缴消息")
    @RequiresPermissions({"admin:fruit:urgeConfirm"})
    public ApiResponse<Void> fruitUrgeConfirm(){
        return sysFruitService.urgeConfirm();
    }

    @PostMapping("/down")
    @ApiOperation("水果基金下载")
    @RequiresPermissions({"admin:fruit:down"})
    public ApiResponse<String> fruitDown(@RequestBody FruitPO po) throws IOException {
        List<FruitExcelVO> vos = sysFruitMapper.fruitDown(po);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, FruitExcelVO.class).sheet("水果基金").doWrite(vos);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "下载水果基金失败"));
        return new ApiResponse<>(UploadFileUrl + s);
    }

    @PostMapping("/count")
    @ApiOperation("获取水果基金统计")
    @RequiresPermissions({"admin:fruit:count"})
    public ApiResponse<FruitCountVO> fruitCount(@RequestBody FruitPO po){
        return sysFruitService.fruitCount(po);
    }

    @PostMapping("/save")
    @ApiOperation("添加水果基金")
    @RequiresPermissions({"admin:fruit:save"})
    public ApiResponse<Void> fruitSave(@RequestBody FruitPO fruitPO){
        return sysFruitService.fruitSave(fruitPO);
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询水果基金")
    @RequiresPermissions({"admin:fruit:getById"})
    public ApiResponse<FruitInfoVO> fruitGetById(@RequestBody FruitPO fruitPO){
        return sysFruitService.fruitGetById(fruitPO);
    }

    @PostMapping("/confirmById")
    @ApiOperation("确认")
    @RequiresPermissions({"admin:fruit:confirmById"})
    public ApiResponse<Void> fruitConfirmById(@RequestBody FruitPO fruitPO){
        return sysFruitService.fruitConfirmById(fruitPO);
    }
}

